/*  
 *  Copyright (c) 2008 Adrian Necimal <necimal@gmail.com>
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Constants
const Cc = Components.classes;
const Ci = Components.interfaces;
 
// make a namespace
if (typeof MusicRecommendations == "undefined") {
  var MusicRecommendations = { };
}

// check that gPPS exists
if (typeof gPPS == "undefined") {
  gPPS = Cc["@songbirdnest.com/Songbird/PlaylistPlayback;1"].getService(Ci.sbIPlaylistPlayback);
}

MusicRecommendations.Pane = {
  onLoad: function() {
    gPPS.addListener(MusicRecommendations.PlayListener);
    this._service = Cc["@necimal.com/recommendations;1"].getService().wrappedJSObject;
  },
  
  onUnload: function() {
    gPPS.removeListener(MusicRecommendations.PlayListener);
  },
  
  showSplash: function() {
    document.getElementById("music-recommendations-list").hidden = true;
    document.getElementById("music-recommendations-splash").hidden = false;
  },
  
  showList: function() {
    document.getElementById("music-recommendations-list").hidden = false;
    document.getElementById("music-recommendations-splash").hidden = true;
  },
  
  // open a url in the browser pane
  openUrl: function(url) {
    window.top.gBrowser.loadURI(url);
  },
  
  showArtistInfo: function(reader) {
    // display some basic information
    document.getElementById("artist-name").firstChild.nodeValue = reader.name;
    document.getElementById("artist-description").firstChild.nodeValue = reader.summary;
    
    // load the image and handle its click event
    var imgNode = document.getElementById("artist-img");
    imgNode.src = reader.image;
    imgNode.setAttribute("onclick", "MusicRecommendations.Pane.openUrl('" + reader.url + "');");
    
    // list the recommended artists
    var artistNodes = document.getElementById("similar-artists-box").childNodes;
    
    for (i=0; i < artistNodes.length; i++) {
      var nextArtist = artistNodes.item(i);
      
      if (i < reader.similar.length) {
        nextArtist.childNodes.item(0).src = reader.similar[i].image;
        nextArtist.childNodes.item(1).value = reader.similar[i].name;
        nextArtist.setAttribute("onclick", "MusicRecommendations.Pane.openUrl('" + reader.similar[i].url + "');");
        
        nextArtist.collapsed = false;
      }
      else {
        nextArtist.collapsed = true;
      }
    }
  }
};

MusicRecommendations.PlayListener = {
  onTrackChange: function() {
    var mediaItem = gPPS.playingView.mediaList.getItemByGuid(gPPS.currentGUID);
    var artist = mediaItem.getProperty("http://songbirdnest.com/data/1.0#artistName");
    
    var reader = MusicRecommendations.Pane._service.query(artist);
    if (reader) {
      // display the artist info & hide the splash screen
      MusicRecommendations.Pane.showArtistInfo(reader);
      MusicRecommendations.Pane.showList();
    } else {
      // show the recommendations splash
      MusicRecommendations.Pane.showSplash();
    }
  },
  
  onStop: function() {
    // show the recommendations splash
    MusicRecommendations.Pane.showSplash();
  },
  
  onBeforeTrackChange: function() { },
  onBeforeViewChange: function(aView) { },
  onViewChange: function(aView) { }
};

// event wireup
window.addEventListener("load", function(e) { MusicRecommendations.Pane.onLoad(e); }, false);
window.addEventListener("unload", function(e) { MusicRecommendations.Pane.onUnload(e); }, false);
